/**
 * Copyright 2011 ArcBees Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package vn.nichecafe.client.view;

import java.util.ArrayList;
import java.util.List;

import vn.nichecafe.client.TabDataExt;
import vn.nichecafe.client.presenter.MainPagePresenter;
import vn.nichecafe.client.ui.Menu;
import vn.nichecafe.client.ui.Slider;

import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.Tab;
import com.gwtplatform.mvp.client.TabData;
import com.gwtplatform.mvp.client.ViewImpl;
import com.gwtplatform.mvp.client.proxy.Gatekeeper;

/**
 * The view implementation for
 * {@link vn.nichecafe.client.presenter.MainPagePresenter}.
 * 
 * @author Christian Goudreau
 * @author Philippe Beaudoin
 */
public class MainPageView extends ViewImpl implements MainPagePresenter.MyView {

	/**
   */
	public interface Binder extends UiBinder<Widget, MainPageView> {
	}

	public final Widget widget;

	Tab currentActiveTab;

	private final List<Menu> tabList = new ArrayList<Menu>();

	@Override
	public Tab addTab(TabData tabData, String historyToken) {
		Menu newTab = createNewTab(tabData);
		int beforeIndex;
		for (beforeIndex = 0; beforeIndex < tabList.size(); ++beforeIndex) {
			if (newTab.getPriority() < tabList.get(beforeIndex).getPriority()) {
				break;
			}
		}
		tabPanel.insert(newTab.asWidget(), beforeIndex);
		tabList.add(beforeIndex, newTab);
		newTab.setTargetHistoryToken(historyToken);
		setTabVisibility(newTab);
		return newTab;
	}

	@Override
	public void removeTab(Tab tab) {
		tabPanel.getElement().removeChild(tab.asWidget().getElement());
		tabList.remove(tab);
	}

	@Override
	public void removeTabs() {
		for (Tab tab : tabList) {
			tabPanel.getElement().removeChild(tab.asWidget().getElement());
		}
		tabList.clear();
	}

	@Override
	public void setActiveTab(Tab tab) {
		if (currentActiveTab != null) {
			currentActiveTab.deactivate();
		}
		if (tab != null) {
			tab.activate();
		}
		currentActiveTab = tab;
		
		// Change slider mode if current tab is welcome
		sliderPanel.setActivateMode(currentActiveTab.getPriority() == 0);
		
		// Change header style
		if(currentActiveTab.getPriority() == 0){
			headerContainner.removeStyleName("header-detail-style");
			contentInside.removeStyleName("content-detail-style");
		}else{
			headerContainner.addStyleName("header-detail-style");
			contentInside.addStyleName("content-detail-style");
		}
	}

	@Override
	public void changeTab(Tab tab, TabData tabData, String historyToken) {
		tab.setText(tabData.getLabel());
		tab.setTargetHistoryToken(historyToken);
	}

	/**
	 * Sets the content displayed in the main panel.
	 * 
	 * @param panelContent
	 *            The {@link Widget} to set in the main panel, or {@code null}
	 *            to clear the panel.
	 */
	public void setPanelContent(Widget panelContent) {
		tabContentContainer.clear();
		if (panelContent != null) {
			tabContentContainer.add(panelContent);
		}
	}

	/**
	 * Ensures that all tabs are visible or hidden as they should.
	 */
	public void refreshTabs() {
		for (Menu tab : tabList) {
			setTabVisibility(tab);
		}
	}

	/**
	 * Ensures the specified tab is visible or hidden as it should.
	 * 
	 * @param tab
	 *            The {@link BaseTab} to check.
	 */
	private void setTabVisibility(Menu tab) {
		
		boolean visible = (tab == currentActiveTab) || tab.canUserAccess();
		tab.setVisible(visible);
	}

	/**
	 * Returns a new tab of the type specific for this tab panel.
	 * 
	 * @param tabData
	 *            Some data on the tab to create.
	 * @return The new tab.
	 */
	protected Menu createNewTab(TabData tabData) {
		Gatekeeper gatekeeper = null;
		if (tabData instanceof TabDataExt) {
			TabDataExt tabDataExt = (TabDataExt) tabData;
			gatekeeper = tabDataExt.getGatekeeper();
		}

		// TODO Try using assisted injection here (to inject UiBinder in
		// RoundTab)
		return new Menu(tabData, gatekeeper);
	}

	@UiField
	FlowPanel tabPanel;

	@UiField
	Slider sliderPanel;
	
	@UiField
	FlowPanel tabContentContainer;
	
	@UiField
	HTMLPanel headerContainner;
	
	@UiField
	HTMLPanel contentInside;
	
	@Inject
	public MainPageView(Binder uiBinder) {
		widget = uiBinder.createAndBindUi(this);
	}

	@Override
	public Widget asWidget() {
		return widget;
	}

	@Override
	public void setInSlot(Object slot, Widget content) {
		if (slot == MainPagePresenter.TYPE_SetTabContent) {
			tabContentContainer.clear();
			if (content != null) {
				content.setStyleName("content-containner");
				tabContentContainer.add(content);
			}
		} else {
			content.setStyleName("content-containner");
			super.setInSlot(slot, content);
		}
	}

	@Override
	public void setTopMessage(String string) {
		if (string == null || string.length() == 0) {
		}
	}
}